home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_gen / wdj0796.zip / CC.ZIP / COMMON.C < prev    next >
Text File  |  1993-10-16  |  6KB  |  214 lines

  1. typedef struct  FileType
  2.     {
  3.     const char    *Extension;
  4.     char    **Array;
  5.     int     NFiles;
  6.     }           FileType;
  7.  
  8.  
  9. int     FileExists(const char *PathName)
  10.     {
  11.     int     Exists;
  12.     FILE    *This = fopen(PathName, "r");
  13.     Exists  = (This != NULL);
  14.     fclose(This);
  15.     return Exists;
  16.     }
  17.  
  18.  
  19. void    PutString(int Device, char *String)
  20.     {
  21.     WRITE(Device, String, strlen(String));
  22.     }
  23. void    PutDec(int Device, int Value)
  24.     {
  25.     char    String[2];
  26.  
  27.     if(Value >= 10)
  28.         PutDec(Device, Value/10);
  29.     String[0]   = '0' + Value%10;
  30.     String[1]   = '\0';
  31.     PutString(Device, String);
  32.     }
  33.  
  34. /* ErrorOutput() - write string to stderr */
  35. int ErrorCount;
  36. void    ErrorOutput(const char *String)
  37.     {
  38.     WRITE(2, String, strlen(String));
  39.     ++ErrorCount;
  40.     }
  41.  
  42.  
  43.  
  44. /* FindFileName() - return point to filename in path */
  45. char    *FindFileName(const char *Path)
  46.     {
  47.     const char  *BackSlash;
  48.     const char  *ForwardSlash;
  49.  
  50.     ForwardSlash    = strrchr(Path, '/');
  51.     if(ForwardSlash)
  52.         ++ForwardSlash;
  53.     else
  54.         ForwardSlash    = Path;
  55.     BackSlash   = strrchr(ForwardSlash, '\\');
  56.     if(BackSlash)
  57.         return (char *)(BackSlash+1);
  58.     else
  59.         return (char *)ForwardSlash;
  60.     }
  61.  
  62. /* FindExtension() - return pointer to extension in filename */
  63. char    *FindExtension(const char *Path)
  64.     {
  65.     const char  *FileName;
  66.     const char  *Dot;
  67.  
  68.     FileName    = FindFileName(Path);
  69.     Dot         = strrchr(FileName, '.');
  70.  
  71.     if(Dot)
  72.         return (char *)(Dot + 1);
  73.     else
  74.         return (char *)(FileName + strlen(FileName));
  75.     }
  76.  
  77. void    EchoArgs(char **Args, int NArgs)
  78.     {
  79.     int     i;
  80.  
  81.     for(i = 0; i < NArgs; ++i)
  82.         fprintf(stdout, "%s%s", i?" ":"", Args[i]);
  83.     fprintf(stdout, "\n");
  84.     }
  85.  
  86. void    CopyFileName(const char *From, char *To)
  87.     {
  88.     From    = FindFileName(From);
  89.     while(*From && *From != '.')
  90.         *To++   = *From++;
  91.     *To++   = '\0';
  92.     }
  93.  
  94. void    CopyPathName(const char *From, char *To)
  95.     {
  96.     const char *File = FindFileName(From);
  97.     while(From < File)
  98.         *To++   = *From++;
  99.     CopyFileName(From, To);
  100.     }
  101. /* SortFiles() - produce sorted list of files by extension */
  102. void    SortFiles(char **Input, FileType *Output,
  103.     const char *Extension, const char *Default)
  104.     {
  105.     char    **Files;
  106.     int     DoingDefault;
  107.     int     Sorted = 0;
  108.  
  109.     while(*Extension)
  110.         {
  111.         Output->NFiles      = 0;
  112.         Output->Array       = Input + Sorted;
  113.         Output->Extension   = Extension;
  114.         DoingDefault        = !STRICMP(Extension, Default);
  115.         for(Files = Input; *Files; ++Files)
  116.             {
  117.             if(!STRICMP(Extension, FindExtension(*Files))
  118.                 ||(DoingDefault && !STRICMP("", FindExtension(*Files))))
  119.                 {
  120.                 char    *Temp;
  121.                 Temp            = Input[Sorted];
  122.                 Input[Sorted]   = *Files;
  123.                 *Files          = Temp;
  124.                 ++Sorted;
  125.                 ++Output->NFiles;
  126.                 }
  127.             }
  128.         ++Output;
  129.         Extension   += strlen(Extension)+1;
  130.         }
  131.     }
  132.  
  133.  
  134. #if 0
  135. int     OpenForWrite(const char *FileName)
  136.     {
  137.     int     FileHandle;
  138.  
  139.     FileHandle  = OPEN(FileName, O_RDWR|O_CREAT|O_TRUNC, S_IWRITE|S_IREAD);
  140.     if(FileHandle < 0)
  141.         {
  142.         ErrorOutput("Can't create file '");
  143.         ErrorOutput(FileName);
  144.         ErrorOutput("'!\r\n");
  145.         exit(EXIT_FAILURE);
  146.         }
  147.     return FileHandle;
  148.     }
  149.  
  150. #endif
  151. void    GenModDef(char *Path, int Library)
  152.     {
  153.     FILE    *ModFile;
  154.     char    FileName[16];
  155.  
  156.     CopyFileName(Path, FileName);
  157.     strcat(FileName, ".def");
  158.     ModFile = fopen(FileName, "r");
  159.     if(ModFile != NULL) /* if file already exists, use it */
  160.         fclose(ModFile);
  161.     else
  162.         {
  163.         ModFile = fopen(FileName, "w");
  164.         if(ModFile == NULL)
  165.             {
  166.             fprintf(stderr, "Can't create '%s'.\n", FileName);
  167.             exit(EXIT_FAILURE);
  168.             }
  169.         else
  170.             {
  171.             CopyFileName(Path, FileName);
  172.             fprintf(ModFile, "%-14.14s%s\n", Library ? "LIBRARY"
  173.                 : "NAME", FileName);
  174.             fprintf(ModFile, "DESCRIPTION  'Default module "
  175.                 "definition file for %s.%s.'\n",
  176.                 FileName, Library ? "lib" : "exe" );
  177.             fprintf(ModFile, "EXETYPE       WINDOWS\n");
  178.             fprintf(ModFile, "CODE          PRELOAD MOVEABLE DISCARDABLE\n");
  179.             fprintf(ModFile, "DATA          PRELOAD MOVEABLE %s\n",
  180.                 Library ? "SINGLE" : "MULTIPLE" );
  181.             fprintf(ModFile, "HEAPSIZE      4096\n");
  182.             if(!Library)
  183.                 fprintf(ModFile, "STACKSIZE     16384\n");
  184.             else
  185.                 {
  186. #if 0 /* we're assuming you're using __export and want to allow
  187.          you to create a DLL that does not have a WEP  */  
  188.                 fprintf(ModFile, "EXPORTS\n");
  189.                 fprintf(ModFile, "    WEP           @1       RESIDENTNAME\n");
  190. #endif
  191.                 }
  192.             fclose(ModFile);
  193.             }
  194.         }    
  195.     }
  196.  
  197. char    *SearchPath(const char *Paths, const char *File, char *Path)
  198.     {
  199.     const char *Rover = Paths;
  200.     while(*Rover)
  201.         {
  202.         char    *SearchPath = Path;
  203.         while(*Rover && *Rover != ';')
  204.             *SearchPath++   = *Rover++;
  205.         *SearchPath++ = '\\';
  206.         strcpy(SearchPath, File);
  207.         if(FileExists(Path))
  208.             return Path;
  209.         else if(*Rover == ';')
  210.             ++Rover;
  211.         }
  212.     return NULL;
  213.     }
  214.